package com.bestcem.xm.award.dao.impl.mysql;

import com.bestcem.xm.award.dao.AwardRequestStatsDayDao;
import com.bestcem.xm.award.dao.mapper.AwardRequestStatsDayMapper;
import com.bestcem.xm.award.entity.mysql.AwardRequestStatsDayEntity;
import com.bestcem.xm.award.entity.pojo.AwardRequestStatsDayDO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/23 11:28
 */
@Repository
public class AwardRequestStatsDaoImpl implements AwardRequestStatsDayDao {

    @Resource
    protected AwardRequestStatsDayMapper awardRequestStatsDayMapper;

    @Override
    public String insert(AwardRequestStatsDayDO awardRequestStatsDayDO) {
        AwardRequestStatsDayEntity entity = toEntity(awardRequestStatsDayDO);
        String id = StringUtil.getId();
        entity.setStatsId(id);
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        awardRequestStatsDayMapper.insert(entity);
        return id;
    }

    @Override
    public Integer updateByAwardIdAndStatsDay(AwardRequestStatsDayDO awardRequestStatsDayDO) {
        AwardRequestStatsDayEntity entity = toEntity(awardRequestStatsDayDO);
        String id = StringUtil.getId();
        entity.setStatsId(id);
        entity.setUpdateTime(DateUtil.getCommonDate());
        return awardRequestStatsDayMapper.updateByAwardIdAndStatsDay(entity);
    }

    @Override
    public List<AwardRequestStatsDayDO> listAwardRequestStatsDays(String awardId, Date startDate, Date endDate) {
        List<AwardRequestStatsDayEntity> awardRequestStatsDayEntityList = awardRequestStatsDayMapper.listAwardRequestStatsDays(awardId, startDate, endDate);
        return listToDO(awardRequestStatsDayEntityList);
    }

    @Override
    public Integer batchInsert(List<AwardRequestStatsDayDO> awardRequestStatsDayDOS) {
        if (CollectionUtils.isEmpty(awardRequestStatsDayDOS)) {
            return 0;
        }
        List<AwardRequestStatsDayEntity> entityList = awardRequestStatsDayDOS.stream().map(this::toEntity).collect(Collectors.toList());
        entityList.stream().forEach(awardRequestStatsDayEntity -> {
            awardRequestStatsDayEntity.setStatsId(StringUtil.getId());
            awardRequestStatsDayEntity.setCreateTime(DateUtil.getCommonDate());
            awardRequestStatsDayEntity.setUpdateTime(DateUtil.getCommonDate());
        });
        return awardRequestStatsDayMapper.batchInsert(entityList);
    }

    @Override
    public Integer deleteByStatsDay(Date statsDay) {
        return awardRequestStatsDayMapper.deleteByStatsDay(statsDay);
    }

    /**
     * DO -> entity
     *
     * @param awardRequestStatsDayDO DO
     * @return entity
     */
    private AwardRequestStatsDayEntity toEntity(AwardRequestStatsDayDO awardRequestStatsDayDO) {
        AwardRequestStatsDayEntity entity = new AwardRequestStatsDayEntity();
        entity.setOrgId(awardRequestStatsDayDO.getOrgId());
        entity.setProjectId(awardRequestStatsDayDO.getProjectId());
        entity.setAwardId(awardRequestStatsDayDO.getAwardId());
        entity.setTotal(awardRequestStatsDayDO.getTotal());
        entity.setPass(awardRequestStatsDayDO.getPass());
        entity.setStatsDay(awardRequestStatsDayDO.getStatsDay());
        return entity;
    }

    /**
     * list : entity -> DO
     *
     * @param awardRequestStatsDayEntityList entityList
     * @return DOList
     */
    private List<AwardRequestStatsDayDO> listToDO(List<AwardRequestStatsDayEntity> awardRequestStatsDayEntityList) {
        return awardRequestStatsDayEntityList.stream().map(this::toDO).collect(Collectors.toList());
    }

    /**
     * entity -> DO
     *
     * @param awardRequestStatsDayEntity entity
     * @return DO
     */
    private AwardRequestStatsDayDO toDO(AwardRequestStatsDayEntity awardRequestStatsDayEntity) {
        AwardRequestStatsDayDO awardRequestStatsDayDO = new AwardRequestStatsDayDO();
        awardRequestStatsDayDO.setId(awardRequestStatsDayEntity.getStatsId());
        awardRequestStatsDayDO.setOrgId(awardRequestStatsDayEntity.getOrgId());
        awardRequestStatsDayDO.setProjectId(awardRequestStatsDayEntity.getProjectId());
        awardRequestStatsDayDO.setAwardId(awardRequestStatsDayEntity.getAwardId());
        awardRequestStatsDayDO.setTotal(awardRequestStatsDayEntity.getTotal());
        awardRequestStatsDayDO.setPass(awardRequestStatsDayEntity.getPass());
        awardRequestStatsDayDO.setStatsDay(awardRequestStatsDayEntity.getStatsDay());
        awardRequestStatsDayDO.setCreateTime(awardRequestStatsDayEntity.getCreateTime());
        awardRequestStatsDayDO.setUpdateTime(awardRequestStatsDayEntity.getUpdateTime());
        return awardRequestStatsDayDO;
    }
}
